
// console.log($)
// $.ajax()

function format_time(_time) {
    /*
    * 分钟 < 60
    * 小时 < 24小时
    * 天
    * */
    // let time = new Date() - new Date(item.ptime)
    let time = new Date().getTime() - new Date(_time).getTime()
    if (time < 1000 * 60 * 60) {
        return parseInt(time / 1000 / 60) + '分钟'
    } else if (time < 1000 * 60 * 60 * 24) {
        return parseInt(time / 1000 / 60 / 60) + '小时'
    } else {
        return parseInt(time / 1000 / 60 / 60 / 24) + '天'
    }
}

function format_comment(cCount) {
    /*
    * 300
    * 2k+
    * 1w+
    * */
    if (cCount < 1000) {
        return cCount
    } else if (cCount < 10000) {
        return parseInt(cCount / 1000) + 'k+'
    } else if (cCount > 10000) {
        return parseInt(cCount / 10000) + 'w+'
    }
}


function getQuery(key) {
    let queryArr = location.search.slice(1).split('&')
    for (let i = 0; i < queryArr.length; i++) {
        let item = queryArr[i].split('=')
        if (item[0] === key) {
            return item[1]
        }
    }
}


let menuData = [
    { text: '娱乐', type: 'BA10TA81wangning' },
    { text: '娱乐', type: 'BA10TA81wangning' },
    { text: '娱乐', type: 'BA10TA81wangning' },
    { text: '娱乐', type: 'BA10TA81wangning' },
    { text: '娱乐', type: 'BA10TA81wangning' },
    { text: '娱乐', type: 'BA10TA81wangning' },
    { text: '娱乐', type: 'BA10TA81wangning' },
]




// console.log(getQuery('type'))
const content = document.querySelector('.content')
let type = getQuery('type') || 'BA10TA81wangning'
// console.log(type)

jQuery.ajax({
    method: 'GET',
    url: `https://3g.163.com/touch/reconstruct/article/list/${type}/20-20.html`,
    dataType: 'jsonp',
    jsonpCallback: 'artiList',
    success(res) {
        const list = res[type]
        list.forEach(function (item) {
            content.insertAdjacentHTML('beforeend', `
                <a href="${item.url || item.skipURL}" class="link">
                    <div class="news-info">
                        <div class="text">
                            <h3>${item.title}</h3>
                            <div class="detail">
                                <span>${item.source}</span>
                                <span>${format_time(item.ptime)}前</span>
                                <span>${format_comment(item.commentCount)}跟贴</span>
                            </div>
                        </div>
                        <div class="pic" style="background-image: url(${item.imgsrc})"></div>
                    </div>
                </a>`)
        })
    }
})

const wapSectionAs = document.querySelectorAll('.wap-section li a')
// wapSectionAs[0].classList.add('active')
wapSectionAs.forEach(function (item) {
    if (item.getAttribute('data-type') === type) {
        item.classList.add('active')
    }

    item.addEventListener('click', function () {
        /*wapSectionAs.forEach(function (item) {
            item.classList.remove('active')
        })
        this.classList.add('active')*/

        const type = this.getAttribute('data-type')
        location.href = `./index.html?type=${type}`
    })
})




